<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闭包实现按钮移动动画</title>
    <style>
        button{
            position: absolute;
        }
    </style>
</head>
<body>
    <button>xiasnn</button>
    <button>hdr</button>
    <script>
        let bts = document.querySelectorAll('button');
        bts.forEach(item => {
            // 把left和interval放到外面
            // let left = 1;
            // let interval = false;
            // item.addEventListener('click',function(){
            //     if(!interval){
            //         interval = true;
            //         setInterval(() => {
            //             item.style.left = ++left + 'px';
            //         }, 50);
            //     }
            // })


            // 把left放到click事件里面
            let bind = false;
            item.addEventListener('click',function(){
                if(!bind){
                    let left = 1;
                    bind = true;
                    setInterval(() => {
                        item.style.left = ++left + 'px';
                    }, 50);
                }
            })
        })
    </script>
</body>
</html>